/**
 * 
 */
package com.gomeplus.frame.template;

import com.gomeplus.frame.cache.GlobalApplicationCache;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author chenmin-ds
 * 标记编码转换成中文展示
 */
public class CodeToTalTemplate implements TemplateMethodModel{

	private Logger logger = LoggerFactory.getLogger("CodeToTalTemplate");
	
	/**
	 * [0] 标记编码
	 * [1] 标签类型    div  /  span
	 * [2] 样式名称  
	 */
	@Override
	@SuppressWarnings("rawtypes")
	public Object exec(List arguments) throws TemplateModelException {
		try{
			String numbers = (String) arguments.get(0);
			String type = (String) arguments.get(1);
			String className = (String) arguments.get(2);
			if(StringUtils.isNotEmpty(numbers) && numbers.split(",").length > 0){
				StringBuffer sb = new StringBuffer();
				String code = "";
				for(String temp : numbers.split(",")){
					code = GlobalApplicationCache.getInstance().getStr(temp);
					if(StringUtils.isNotEmpty(code)){
						sb.append("<");
						sb.append(type);
						sb.append(" class='");
						sb.append(className);
						sb.append("' >");
						sb.append(GlobalApplicationCache.getInstance().getStr(temp));
						sb.append("</");
						sb.append(type);
						sb.append(">");
					}
				}
				return sb.toString();
			}
		}catch(Exception e){
			logger.error("标记编码转换成中文展示异常：{}", e.getMessage());
		}
		return "";
	}

}
